package edu.sctu.yueding.fragment.myorder;


import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.RelativeLayout;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.android.volley.Response;
import com.android.volley.VolleyError;

import java.util.ArrayList;
import java.util.List;

import edu.sctu.yueding.R;
import edu.sctu.yueding.activitys.MyOrderMActivity;
import edu.sctu.yueding.adapter.MyOrderAdapter;
import edu.sctu.yueding.bean.JsonBaseBeen;
import edu.sctu.yueding.bean.JsonBaseListBean;
import edu.sctu.yueding.bean.OrderDetialBean;
import edu.sctu.yueding.bean.OrderInfoBean;
import edu.sctu.yueding.config.CacheConfig;
import edu.sctu.yueding.config.URLConfig;
import edu.sctu.yueding.utils.DateUtils;
import edu.sctu.yueding.utils.JumpUtils;
import edu.sctu.yueding.utils.httputils.GetVolleyHttp;
import edu.sctu.yueding.utils.httputils.SharePreference;
import edu.sctu.yueding.view.XListView;

/**
 * Created by Administrator on 2016/9/21.
 */
public class MyOrderFragment extends Fragment implements XListView.IXListViewListener,AdapterView.OnItemClickListener {


    private XListView listView;
    private Activity mActivity;
    private RelativeLayout mProgressBarRel;
    private JsonBaseListBean<OrderInfoBean> orderBeanJsonBaseListBean = null;

    private int page = 0;
    private List<OrderInfoBean> datas = new ArrayList<>();

    private List<OrderInfoBean> waitPay;
    private List<OrderInfoBean> payed;


    private MyOrderAdapter adapter = null;
    private boolean isAddAll = true;
    View view = null;
    private int flag;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        if (view == null) {
            view = inflater.inflate(R.layout.fragment_my_order, container, false);
            initView();
        }


        return view;
    }

    private void initView() {

        Log.e("555", "5555");
        /**
         * 重置状态 防止第二次进入 数据获取发生误差
         */
        adapter = null;

        flag = getArguments().getInt("flag");
        mActivity = getActivity();
        mProgressBarRel = (RelativeLayout) view.findViewById(R.id.my_order_list_progress_bar);
        listView = (XListView) view.findViewById(R.id.my_order_list);

        listView.setPullLoadEnable(false);
        listView.setPullRefreshEnable(true);
        listView.setXListViewListener(this);


        if (orderBeanJsonBaseListBean == null) {

//            new Handler().postDelayed(new Runnable() {
//                @Override
//                public void run() {
            initData();
//                }
//            }, 1000);
        } else {
            mProgressBarRel.setVisibility(View.GONE);
        }

        listView.setOnItemClickListener(this);

    }


    private void initData() {

        GetVolleyHttp.getInstance(mActivity).getStringGETJsonData(URLConfig.MY_ORDERS,
                listener, errorListener, "Bearer " + SharePreference.getInstance(mActivity).getString(CacheConfig.CACHE_TOKEN));
    }

    Response.Listener listener = new Response.Listener<String>() {
        @Override
        public void onResponse(String json) {
            mProgressBarRel.setVisibility(View.GONE);
            Log.e("json----------", json);

            orderBeanJsonBaseListBean = JSON.parseObject(json, new TypeReference<JsonBaseListBean<OrderInfoBean>>() {
            }.getType());

            if (orderBeanJsonBaseListBean.getMeta().code == 200) {

                datas = orderBeanJsonBaseListBean.getData();

                if (datas != null) {

                    payed = new ArrayList<>();

                    waitPay = new ArrayList<>();

                    for (OrderInfoBean orderBean : datas) {

                        if (orderBean.getStatus() == 0) {

                            payed.add(orderBean);

                        } else if (orderBean.getStatus() == 3) {

                            waitPay.add(orderBean);

                        }
                    }
                    initAdapter();
                }
            }


        }
    };
    Response.ErrorListener errorListener = new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            error.printStackTrace();
            mProgressBarRel.setVisibility(View.GONE);
        }
    };


    @Override
    public void onRefresh() {
        page = 0;
        isAddAll = false;
        initData();

    }

    @Override
    public void onLoadMore() {
        isAddAll = true;
        initData();
    }

    private void initAdapter() {
        if (adapter == null) {

            if (flag == 1) {

                adapter = new MyOrderAdapter(mActivity, waitPay, flag);

            } else if (flag == 0) {

                adapter = new MyOrderAdapter(mActivity, payed, flag);
            }

            listView.setAdapter(adapter);
        } else {

            if (flag == 1) {

                adapter.fresh(waitPay, false);

            } else if (flag == 0) {

                adapter.fresh(payed, false);
            }


        }

        stop();
    }

    private void stop() {
        listView.stopLoadMore();
        listView.stopRefresh();
        listView.setRefreshTime(DateUtils.getTime());
    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {


        OrderInfoBean orderInfoBean = (OrderInfoBean) adapter.getItem(i-1);

        GetVolleyHttp.getInstance(getActivity()).getStringGETJsonData(URLConfig.getMyOrderDetil(orderInfoBean.getId()), new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {

                JsonBaseBeen<OrderDetialBean> orderDetialBeanJsonBaseBeen = JSON.parseObject(response, new TypeReference<JsonBaseBeen<OrderDetialBean>>() {
                }.getType());

                if (orderDetialBeanJsonBaseBeen.getMeta().code == 200){

                    if (orderDetialBeanJsonBaseBeen.getData() != null){

                        Bundle bundle = new Bundle();
                        bundle.putString("orderDetial",response);
                        JumpUtils.jumpActivity(getActivity(), MyOrderMActivity.class,bundle);
                    }

                }

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        },"");
    }
}
